package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"meituan/utils/config"
)

func UploadFile(ctx *gin.Context) {
	loadConfig, _ := config.LoadConfig()
	file, err := ctx.FormFile("file")
	if err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  "上传失败",
		})
		return
	}
	if err := ctx.SaveUploadedFile(
		file,
		fmt.Sprintf(
			"%s/%s",
			loadConfig.Gin.Upload.Path,
			file.Filename,
		),
	); err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  "上传失败",
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": fmt.Sprintf("/uploads/%s", file.Filename),
	})

}
